package com.crm.service.repositoryService;

import com.crm.dao.productionDAO.ProductionDetilsDAO;
import com.crm.dao.purchaseDAO.PurchaseproductiondetailDAO;
import com.crm.dao.saling.DispatcherDetailsDao;
import com.crm.dao.saling.RefundDetailsDao;
import com.crm.pojo.ProductionDetail;
import com.crm.vo.ProstockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ProstockService {
    @Autowired
    private ProductionDetilsDAO productionDetilsDAO;
    @Autowired
    private PurchaseproductiondetailDAO purchaseproductiondetailDAO;
    @Autowired
    private DispatcherDetailsDao dispatcherDetailsDao;
    @Autowired
    private RefundDetailsDao refundDetailsDao;

    //获取所有的产品信息(未删除)
    public List<ProductionDetail> queryAll(PageRequest pageRequest){
        return productionDetilsDAO.queryAll(pageRequest);
    }
    //通过产品proid查询采购状态为采购完成-已入库下的状态为否和待退货采购产品明细中的采购总数
    public Long queryCountP(Long proid){
        Long count = purchaseproductiondetailDAO.queryCountByproid(proid);
        if(count==null){
            return 0L;
        }else{
            return count;
        }
    }
    //通过产品proid查询发货单状态为已发货下的发货明细下的已经审核通过的数量
    public Long queryCountD(Long proid){
        Long count = dispatcherDetailsDao.queryCountByproid(proid);
        if(count==null){
            return 0L;
        }else{
            return count;
        }
    }
    //通过产品proid查询退货单状态为已收货下的退货明细下的已经审核通过的数量
    public Long queryCountT(Long proid){
       Long count = refundDetailsDao.queryCountByproid(proid);
        if(count==null){
            return 0L;
        }else{
            return count;
        }
    }
    public List<ProstockVO> findAll(int curpage, int pagesize){
        PageRequest pageRequest = new PageRequest(curpage-1,pagesize);
        List<ProductionDetail> productionDetails = this.queryAll(pageRequest);
        return this.dofor(productionDetails);
    }
    public List<ProstockVO> findAllBymh(String q){
       List<ProductionDetail> plist = productionDetilsDAO.queryBypidOrpnameMh1("%"+q+"%");
       return this.dofor(plist);
    }
    public List<ProstockVO> dofor(List<ProductionDetail> productionDetails){
        List<ProstockVO> list = new ArrayList<>();
        for (ProductionDetail p:productionDetails) {
            ProstockVO vo = new ProstockVO();
            vo.setProId(p.getPid());
            vo.setProImg(p.getPimg());
            vo.setProName(p.getPname());
            //入库数-出库数
            vo.setProNum(this.queryCountP(p.getPid())-this.queryCountD(p.getPid())+this.queryCountT(p.getPid()));
            p.setPstock(this.queryCountP(p.getPid())-this.queryCountD(p.getPid())+this.queryCountT(p.getPid()));
            productionDetilsDAO.save(p);
            list.add(vo);
        }
        return list;
    }

}
